from django.shortcuts import render
from django.template import RequestContext
from .models import Article, Tag, Classification
from django.shortcuts import redirect
# Create your views here.

def blog_list(request):
    blogs = Article.objects.all().order_by('-publish_time')
    return render(request, 'index.html',{"blogs":blogs})#maybe

def blog_detail(request):
    if request.method == 'GET':
        id = request.GET.get('id', '');
        try:
            blog = Article.objects.get(id=id)
        except Article.DoesNotExist:
            raise Http404
        return render(request, "detail.html",{"blog":blog})
    else:
        raise Http404

def blog_about(request):
    return render(request, 'about.html')

def blog_contact(request):
    return render(request, 'contact.html')

def tag_detail(request, tag_slug):
    tags = Tag.objects.all()
    tag = Tag.objects.get(slug=tag_slug) 
    return render(request, 'tag.html', {'tag': tag})

def classification_detail(request, classification_slug):
    classifications = Classification.objects.all()
    classification = Classification.objects.get(slug=classification_slug)
    return render(request, 'classification.html', {'classification': classification,'classifications': classifications})

def article_detail(request, article_slug):
    article = Article.objects.get(slug=article_slug)
    return render(request, 'detail.html', {'article': article})

def index(request):
    classifications = Classification.objects.all()
    return render(request, 'base.html', {'classifications':classifications})


